package com.usian.wemedia.filter;

import com.usian.utils.threadlocal.ThreadLocalUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.core.config.Order;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// @Order(1)
// @WebFilter(filterName = "WmTokenFilter",urlPatterns = "/*")
// @Log4j2
//全局过滤器过滤到该微服务
public class WmTokenFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //得到header中的信息
        String userId = request.getHeader("userId");
        if(userId!=null){
            ThreadLocalUtils.setUserId(Integer.parseInt(userId));
        }
        //放行
        filterChain.doFilter(request,response);
    }
}
